//
//  NumCountView.m
//  HDYS
//
//  Created by qianfeng on 15-4-21.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "NumCountView.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

@interface NumCountView () <UITextFieldDelegate>
{
    NSInteger numCount;
}

@property (nonatomic, strong) UITextField *textField;

@end

@implementation NumCountView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createUI];
    }
    return self;
}

-(void)createUI{
//    NSLog(@"%@",NSStringFromCGRect(self.frame));
    
    UIButton *decrementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    decrementBtn.tag = 100;
    decrementBtn.frame = CGRectMake(0, 0, self.frame.size.width / 3, self.frame.size.height);
    [decrementBtn setTitle:@"减小" forState:UIControlStateNormal];
    decrementBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [decrementBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [decrementBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:decrementBtn];
    
    
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(decrementBtn.frame.origin.x + decrementBtn.frame.size.width, 0, decrementBtn.frame.size.width, decrementBtn.frame.size.height)];
    textField.text = @"0";
    textField.delegate = self;
    [self addSubview:textField];
    self.textField = textField;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    
    //设置附加视图
    UIView *accesoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    accesoryView.backgroundColor = [UIColor grayColor];
    UIButton *finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    CGFloat finishBtnW = 40;
    finishBtn.frame = CGRectMake(SCREEN_WIDTH - finishBtnW - 10, 5, finishBtnW, 30);
    [finishBtn setTitle:@"完成" forState:UIControlStateNormal];
    [finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [accesoryView addSubview:finishBtn];
    
    textField.inputAccessoryView = accesoryView;
    
    UIButton *crementBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    crementBtn.frame = CGRectMake(textField.frame.origin.x + textField.frame.size.width, 0, textField.frame.size.width, textField.frame.size.height);
    crementBtn.tag = 101;
    [crementBtn setTitle:@"增加" forState:UIControlStateNormal];
    [crementBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    crementBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    [crementBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:crementBtn];
}

#pragma mark - textField协议方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if ([self.delegate respondsToSelector:@selector(numCountViewDidBeginEdting:)]) {
        [self.delegate numCountViewDidBeginEdting:self];
    }
}

#pragma mark - 按钮点击
-(void)btnClick:(UIButton *)btn{
    if (btn.tag - 100) {
        //增加
        numCount++;
    } else{
        //减少
        if (numCount != 0) {
            numCount--;
        }
    }
    self.textField.text = [NSString stringWithFormat:@"%ld",numCount];
}

-(void)finishBtnClick{
    [self.textField resignFirstResponder];
}

@end
